<?php
class App_Source_Groups_Validator extends Mozg_Property_Validator
{
	protected function ValidateTitle($value)
	{
		return strlen($value);
	}

	protected function ValidateParentId($parentId)
	{
		$obj = $this->GetData();

		$availableGroups = Mozg::User()->GetAvailableGroups();

		$gr = Mozg::User()->GetGrantedGroups();
        $gr[] = Mozg::User()->GetGroup()->GetId();

        if (!in_array($obj->GetParentId(), array_merge($availableGroups, $gr)))
        {
        	$this->SetErrorMessage('parentId', Mozg_Lang::_('PARENT_GROUP_NOT_FOUND'));
        	return false;
        }

        if ($obj->GetId())
        {
	        $subGroups = Mozg_Mapper::Get('Groups')->GetList(array('parentId' => $obj->GetId(), 'levels' => 100));
	        $subGroups[$obj->GetId()] = $obj;
	        if (isset($subGroups[$obj->GetParentId()]))
	        {
				$this->SetErrorMessage('parentId', Mozg_Lang::_('GROUP_CANNOT_CONTAIN_ITS_PARENT_GROUP'));
				return false;
	        }
        }

        return true;
	}
}